package cn.zhentao.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.zhentao.service.TAnnouncementService;
import cn.zhentao.pojo.TAnnouncement;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("announcement")
@CrossOrigin(origins = "*") // 允许跨域访问
public class AnnouncementController {

    @Autowired
    private TAnnouncementService tAnnouncementService;
    
    
        
        /**查询所有*/
        @PostMapping("findList")
        public List<TAnnouncement> findList(@RequestBody TAnnouncement announcement){
            // ... existing code ...
            List<TAnnouncement> records = tAnnouncementService.list(); // 获取所有公告
            return records; // 返回所有公告
        }
    
    
    
    
    
        /**
         * 添加
         * @param announcement 信息
         * @return 添加结果
         */
    
        @PostMapping("add")
        public String add(@RequestBody TAnnouncement announcement) {
            boolean save = tAnnouncementService.save(announcement);
            return save ? "添加成功" : "添加失败";
        }
    
    
    
        /**
         * 修改
         * @param announcement 信息
         * @return 修改结果
         */
        @PostMapping("update")
        public String update(@RequestBody TAnnouncement announcement) {
            boolean update = tAnnouncementService.updateById(announcement);
            return update ? "修改成功" : "修改失败";
        }
    
    
        /**
         * 根据ID查询
         * @param id ID
         * @return 信息
         */
        @GetMapping("findById")
        public TAnnouncement findById(@RequestParam Integer id) {
            return tAnnouncementService.getById(id);
        }
    
        /**
         * 删除
         * @param id ID
         * @return 删除结果
         */
        @GetMapping("delete")
        public String delete(@RequestParam Integer id) {
            boolean delete = tAnnouncementService.removeById(id);
            return delete ? "删除成功" : "删除失败";
        }

        /**
         * 获取最新公告（小程序专用）
         * @return 最新已发布的公告
         */
        @GetMapping("getLatest")
        public Map<String, Object> getLatest() {
            try {
                // 查询最新的已发布公告（status=0表示已发布）
                QueryWrapper<TAnnouncement> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("status", 0) // 只查询已发布的公告
                           .orderByDesc("publish_time") // 按发布时间倒序
                           .last("LIMIT 1"); // 只取最新的一条
                
                TAnnouncement latestAnnouncement = tAnnouncementService.getOne(queryWrapper);
                
                Map<String, Object> response = new HashMap<>();
                if (latestAnnouncement != null) {
                    response.put("code", 200);
                    response.put("message", "获取成功");
                    response.put("data", latestAnnouncement);
                } else {
                    response.put("code", 404);
                    response.put("message", "暂无公告");
                    response.put("data", null);
                }
                
                return response;
                
            } catch (Exception e) {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("code", 500);
                errorResponse.put("message", "获取公告失败: " + e.getMessage());
                errorResponse.put("data", null);
                return errorResponse;
            }
        }

        /**
         * 获取所有有效公告（小程序专用）
         * @return 所有已发布的公告列表
         */
        @GetMapping("getActiveList")
        public Map<String, Object> getActiveList() {
            try {
                // 查询所有已发布的公告
                QueryWrapper<TAnnouncement> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("status", 0) // 只查询已发布的公告
                           .orderByDesc("publish_time"); // 按发布时间倒序
                
                List<TAnnouncement> announcements = tAnnouncementService.list(queryWrapper);
                
                Map<String, Object> response = new HashMap<>();
                response.put("code", 200);
                response.put("message", "获取成功");
                response.put("data", announcements);
                response.put("total", announcements.size());
                
                return response;
                
            } catch (Exception e) {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("code", 500);
                errorResponse.put("message", "获取公告列表失败: " + e.getMessage());
                errorResponse.put("data", null);
                return errorResponse;
            }
        }
    

}
